package com.junchenrobot.mes.listener.source;



import com.junchenrobot.mes.domain.WinderTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Slf4j
@Repository
public class WinderTaskRedisRepositoryImpl implements WinderTaskRedisRepository {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    public WinderTaskRedisRepositoryImpl() {
        log.debug("创建处理缓存的数据访问对象:BrandRedisRepositoryImpl");
    }


    @Override
    public void save(List<WinderTask> winderTasks) {
        String key =WINDER_TASK_LIST_KEY;
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        for (WinderTask winderTask : winderTasks) {
            ops.rightPush(key,winderTask);
        }
    }


    @Override
    public void saveVehicle(List<WinderTask> winderTasks) {
        String key =VEHICLE_WINDER_TASK_LIST_KEY;
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        for (WinderTask winderTask : winderTasks) {
            ops.rightPush(key,winderTask);
        }
    }



    @Override
    public WinderTask deleteVehicleTask(WinderTask winderTask) {
        String key =VEHICLE_WINDER_TASK_LIST_KEY;
        Integer index = 0;
        Serializable serializable = redisTemplate.opsForList().remove(key, index, winderTask);
        WinderTask winderTask1 = null;
        if (serializable != null) {
            if (serializable instanceof WinderTask) {
                winderTask1 =  (WinderTask) serializable;
            }
        }
        return winderTask1;
    }



    @Override
    public WinderTask deleteTask(WinderTask winderTask2) {
        String key =WINDER_TASK_LIST_KEY;
        Integer index = 0;
        Serializable serializable = redisTemplate.opsForList().remove(key, index, winderTask2);
        WinderTask winderTask = null;
        if (serializable != null) {
            if (serializable instanceof WinderTask) {
                winderTask =  (WinderTask) serializable;
            }
        }
        return winderTask;
    }

    @Override
    public Long deleteAll() {
        Set<String> keys =new HashSet<>();
        keys.add(WINDER_TASK_LIST_KEY);
        return redisTemplate.delete(keys);
    }

    @Override
    public WinderTask get(Long id) {
        return null;
    }

    @Override
    public List<WinderTask> list() {
            return null;
    }

    @Override
    public List<WinderTask> list(long start, long end) {
        String key =WINDER_TASK_LIST_KEY;
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(key, start, end);
        List<WinderTask> winderTasks =new ArrayList<>();
        for (Serializable item : list) {
            winderTasks.add((WinderTask) item);
        }
        return winderTasks;
    }

    @Override
    public List<WinderTask> listVehicle(long start, long end) {
        String key =VEHICLE_WINDER_TASK_LIST_KEY;
        ListOperations<String, Serializable> ops = redisTemplate.opsForList();
        List<Serializable> list = ops.range(key, start, end);
        List<WinderTask> winderTasks =new ArrayList<>();
        for (Serializable item : list) {
            winderTasks.add((WinderTask) item);
        }
        return winderTasks;
    }
}
